# -*- coding: utf-8 -*-
# index视图
import math
import os.path

from flask import Blueprint, render_template, abort, current_app
from ..models.tablemodel import Article
from config.settings import PROJECT_ROOT

in_bp = Blueprint('index', __name__)


# =======================原始非静态化处理======================================================#
# @in_bp.route('/')
# def index():
#     article = Article()
#     result = article.find_limit_with_users(0, 10)
#     page_count = math.ceil(article.get_total_count() / 10)
#     return render_template('index.html', result=result, pagecount=page_count, page=1)
#
# @in_bp.route('/page/<int:page>')
# def paginate(page):
#     article = Article()
#     start = (page - 1) * 10
#     result = article.find_limit_with_users(start, 10)
#     page_count = math.ceil(article.get_total_count() / 10)
#     return render_template('index.html', result=result, pagecount=page_count, page=page)

# ========================页面静态化处理===========================================================#
# 访问触发，生成静态页面
# 新增时触发更新：在新增接口（article.）中，直接删除已有静态页面。
@in_bp.route('/')
def index():
    static_path = 'index_static/index_static_1.html'
    if os.path.exists(os.path.join(current_app.root_path, 'templates', static_path)):
        return render_template(static_path)
    else:
        article = Article()
        result = article.find_limit_with_users(0, 10)
        page_count = math.ceil(article.get_total_count() / 10)
        content = render_template('index.html', result=result, pagecount=page_count, page=1)
        with open(os.path.join(current_app.root_path, 'templates', static_path), 'w', encoding='utf-8') as f:
            f.write(content)
        return content


@in_bp.route('/page/<int:page>')
def paginate(page):
    static_path = f'index_static/index_static_{page}.html'
    if os.path.exists(os.path.join(PROJECT_ROOT, 'app/templates', static_path)):
        return render_template(static_path)
    else:
        article = Article()
        start = (page - 1) * 10
        result = article.find_limit_with_users(start, 10)
        page_count = math.ceil(article.get_total_count() / 10)
        content = render_template('index.html', result=result, pagecount=page_count, page=page)
        with open(os.path.join(PROJECT_ROOT, 'app/templates', static_path), 'w', encoding='utf-8') as f:
            f.write(content)
        return content
# ========================页面静态化处理===========================================================#




@in_bp.route('/type/<int:type>-<int:page>')
def classify(type, page):
    article = Article()
    start = (page - 1) * 5
    result = article.find_by_type(type, start, 5)
    page_count = math.ceil(article.get_count_by_type(type) / 5)
    return render_template('index_type.html', result=result, page=page, pagecount=page_count, type=type)


@in_bp.route('/search/<int:page>-<keyword>')
def search_box(page, keyword):
    keyword = keyword.strip()
    # 防止输入非法关键字进行搜索
    if keyword is None or keyword == '' or '%' in keyword or len(keyword) > 10:
        abort(404)
    article = Article()
    start = (page - 1) * 5
    result = article.find_by_searchbox(keyword, start, 5)
    page_count = math.ceil(article.get_count_by_searchbox(keyword) / 5)
    return render_template('index_search.html', result=result, page=page, pagecount=page_count, keyword=keyword)
